package com.simen.hellobytes.image;

/**
 * author: SimenHi
 * create time: 2020-05-12 15:32
 * description:
 */
public class ImageJavaAlgorithmUtil {
    /**
     * 实现高斯模糊
     *
     * @param radius 高斯半径
     */
    public static void blur(int radius, int width, int height, int[] pixels) {
        int wm = width - 1;
        int hm = height - 1;
        int wh = width * height;
        int div = radius + radius + 1;

        int r[] = new int[wh];
        int g[] = new int[wh];
        int b[] = new int[wh];
        int rsum, gsum, bsum, i, p, yp, yi, yw;
        int xPixel, yPixel;
        int vmin[] = new int[Math.max(width, height)];

        int divsum = (div + 1) >> 1;
        divsum *= divsum;
        int temp = 256 * divsum;
        int dv[] = new int[temp];
        for (i = 0; i < temp; i++) {
            dv[i] = (i / divsum);
        }

        yw = yi = 0;

        int[][] stack = new int[div][3];
        int stackpointer;
        int stackstart;
        int[] point;
        int rbs;
        int r1 = radius + 1;
        int routsum, goutsum, boutsum;
        int rinsum, ginsum, binsum;

        for (yPixel = 0; yPixel < height; yPixel++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            for (i = -radius; i <= radius; i++) {
                p = pixels[yi + Math.min(wm, Math.max(i, 0))];
                point = stack[i + radius];
                point[0] = (p & 0xff0000) >> 16;
                point[1] = (p & 0x00ff00) >> 8;
                point[2] = (p & 0x0000ff);
                rbs = r1 - Math.abs(i);
                rsum += point[0] * rbs;
                gsum += point[1] * rbs;
                bsum += point[2] * rbs;
                if (i > 0) {
                    rinsum += point[0];
                    ginsum += point[1];
                    binsum += point[2];
                } else {
                    routsum += point[0];
                    goutsum += point[1];
                    boutsum += point[2];
                }
            }
            stackpointer = radius;

            for (xPixel = 0; xPixel < width; xPixel++) {

                r[yi] = dv[rsum];
                g[yi] = dv[gsum];
                b[yi] = dv[bsum];

                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;

                stackstart = stackpointer - radius + div;
                point = stack[stackstart % div];

                routsum -= point[0];
                goutsum -= point[1];
                boutsum -= point[2];

                if (yPixel == 0) {
                    vmin[xPixel] = Math.min(xPixel + radius + 1, wm);
                }
                p = pixels[yw + vmin[xPixel]];

                point[0] = (p & 0xff0000) >> 16;
                point[1] = (p & 0x00ff00) >> 8;
                point[2] = (p & 0x0000ff);

                rinsum += point[0];
                ginsum += point[1];
                binsum += point[2];

                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;

                stackpointer = (stackpointer + 1) % div;
                point = stack[(stackpointer) % div];

                routsum += point[0];
                goutsum += point[1];
                boutsum += point[2];

                rinsum -= point[0];
                ginsum -= point[1];
                binsum -= point[2];

                yi++;
            }
            yw += width;
        }

        for (xPixel = 0; xPixel < width; xPixel++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            yp = -radius * width;
            for (i = -radius; i <= radius; i++) {
                yi = Math.max(0, yp) + xPixel;

                point = stack[i + radius];

                point[0] = r[yi];
                point[1] = g[yi];
                point[2] = b[yi];

                rbs = r1 - Math.abs(i);

                rsum += r[yi] * rbs;
                gsum += g[yi] * rbs;
                bsum += b[yi] * rbs;

                if (i > 0) {
                    rinsum += point[0];
                    ginsum += point[1];
                    binsum += point[2];
                } else {
                    routsum += point[0];
                    goutsum += point[1];
                    boutsum += point[2];
                }

                if (i < hm) {
                    yp += width;
                }
            }
            yi = xPixel;
            stackpointer = radius;
            for (yPixel = 0; yPixel < height; yPixel++) {
                pixels[yi] = (0xff000000 & pixels[yi]) | (dv[rsum] << 16)
                        | (dv[gsum] << 8) | dv[bsum];

                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;

                stackstart = stackpointer - radius + div;
                point = stack[stackstart % div];

                routsum -= point[0];
                goutsum -= point[1];
                boutsum -= point[2];

                if (xPixel == 0) {
                    vmin[yPixel] = Math.min(yPixel + r1, hm) * width;
                }
                p = xPixel + vmin[yPixel];

                point[0] = r[p];
                point[1] = g[p];
                point[2] = b[p];

                rinsum += point[0];
                ginsum += point[1];
                binsum += point[2];

                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;

                stackpointer = (stackpointer + 1) % div;
                point = stack[stackpointer];

                routsum += point[0];
                goutsum += point[1];
                boutsum += point[2];

                rinsum -= point[0];
                ginsum -= point[1];
                binsum -= point[2];

                yi += width;
            }
        }
    }
}
